#include "solar/trunk/NPushButton.h"

NPushButton::NPushButton(QWidget *parent){
    setParent(parent);
}

void NPushButton::enterEvent(QEnterEvent*){
    emit msev_etr(qMakePair(true, this->objectName()));
}

void NPushButton::leaveEvent(QEvent*){
    emit msev_etr(qMakePair(false, this->objectName()));
}

void NPushButton::mouseReleaseEvent(QMouseEvent *){
    emit msnm_rls(objectName());
}

void NPushButton::setFormat(QString Format, bool Enable){
    if (Enable && !Format.isEmpty()) {
        RdoMap_String["TextFormat"] = Format;
    } else RdoMap_String["TextFormat"] = "";
}

void NPushButton::setFormatText(QString Text, FormatSpecial Format){
    QString FormatChar = RdoMap_String["TextFormat"];
    QRegularExpression BRIREG("%[0-9]{1}|\\?");
    QRegularExpression TXTREG("[\\||\\s+]|\\?");
    QRegularExpression RODREG("[\\<\\|\\(\\[]{1}.*?[\\>\\|\\)\\]]{1}");
    QStringList FormatCharSplit;
    QStringList TextSplit;
    if (FormatChar.isEmpty()){
        QPushButton::setText(Text);
    } else {
        if (Format == FormatSpecial::BrightSingle){
            FormatCharSplit = definfc.getGlobalMatching(FormatChar, BRIREG);

        } else if (Format == FormatSpecial::SpecialSurround){
            FormatCharSplit = definfc.getGlobalMatching(FormatChar, RODREG);
        }
        TextSplit = Text.split(TXTREG);
        int indexofstart = 0;
        if (FormatCharSplit.size() == TextSplit.size()){
            for(int s = 0; s < FormatCharSplit.size(); s++){
                QString fm = FormatCharSplit[s];
                QString spl = TextSplit[s];
                int fmsize = fm.size();
                int charIndex = FormatChar.indexOf(fm, indexofstart + 1);
                FormatChar = FormatChar.replace(charIndex, fmsize, spl);
                indexofstart = charIndex;
            }
        }
    }
    QPushButton::setText(FormatChar);
}
